from context_functions import *
from math import fabs
from math import sqrt
import matplotlib.pyplot as plt
import numpy.numarray as na
import sys

if len(sys.argv)<3:
	print "\n*** usage:",sys.argv[0]," [concept] [dbfile]"
	exit()
else:
	concept = sys.argv[2]
	lsaout = sys.argv[1]

def showDistance(concept,dbfile,path=""):
	m,e,i = readContext(lsaout)

	row = m[e.index(concept)]

	#Average
	cont = 0
	suma = 0
	for elem in row:
		suma = suma+elem
		cont = cont+1

	averageRow = suma/cont

	mapping = {}

	pos = 0
	for elem in row:
		mapping[fabs(elem-averageRow)] = pos
		pos=pos+1

	values = mapping.values()  #  positions
	keys = mapping.keys()  # distances

	keys.sort(reverse=True)

	valuesSort = []
	for k in keys:
		v = mapping[k]
		valuesSort.append(str(v))

	print keys				#distances ordenados
	print valuesSort		#dimensiones ordenados


	plt.plot(valuesSort, keys,marker="o",ls=" ")
	plt.show()
	
def showStandardDeviation(concept,dbfile,path=""):
	m,e,i = readContext(lsaout)

	row = m[e.index(concept)]

	#Average
	cont = 0
	suma = 0
	for elem in row:
		suma = suma+elem
		cont = cont+1

	averageRow = suma/cont
	
	#desv
	suma = 0
	cont = 0
	for elem in row:
		suma = suma + (elem - averageRow)**2
		cont = cont+1
	
	desv = sqrt(suma/cont)
	
	#dentro de desviacion?
	desv_neg = -1*desv
	desv_pos = desv
	print desv
	pos = 0
	y_axis = []
	x_axis = []
	for elem in row:
		if ((elem >= desv_neg) and (elem <= desv_pos)):
			y_axis.append(0)
		else:
			y_axis.append(1)
		x_axis.append(pos)
		pos = pos+1
	print y_axis
	print x_axis
	width = 0.35 #The width of the bars
	xlocations = na.array(range(len(y_axis)))+0.3
	
	
	
	#plt.bar(xlocations, y_axis, width=width)
	
	plt.plot(x_axis, y_axis,marker="o",ls="--")
	plt.show()
	
	
showStandardDeviation(concept,lsaout)